using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class SelectFortressOverStackBlocks : Form
    {
        private struct BlockRemoved
        {
            public Block block;
            public bool removed;
        };

        private Dictionary<PictureBox, BlockRemoved> blocksRemoved;
        private int numBlocksToRemove;
        private int numBlocksSelected = 0;

        public SelectFortressOverStackBlocks(MapInfo.RegionID sorroundingRegion, int numBlocksToRemove, List<Block> blocksInRegion)
        {
            InitializeComponent();
            this.numBlocksToRemove = numBlocksToRemove;
            titleLabel.Text = titleLabel.Text + " " + MapInfo.GetRegionName(sorroundingRegion);
            Point ptToPutPictureBox = new Point((this.Width / 2) - (PlayingPieceGraphics.GetBlockSize().Width / 2), this.titleLabel.Location.Y + titleLabel.Height + 5);
            blocksRemoved = new Dictionary<PictureBox, BlockRemoved>();
            foreach(Block block in blocksInRegion)
            {
                BlockRemoved blockRemoved = new BlockRemoved();
                blockRemoved.block = block;
                blockRemoved.removed = false;
                PictureBox picturebox = new PictureBox();

                picturebox.Location = new Point(ptToPutPictureBox.X, ptToPutPictureBox.Y);
                picturebox.Size = PlayingPieceGraphics.GetBlockSize();

                ptToPutPictureBox.Y += PlayingPieceGraphics.GetBlockSize().Height + 5;
                picturebox.Paint += new PaintEventHandler(picturebox_Paint);
                picturebox.Click += new EventHandler(picturebox_Click);
                this.Controls.Add(picturebox);
                blocksRemoved.Add(picturebox, blockRemoved);
            }

            Button okButton = new Button();
            okButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            okButton.Font = new System.Drawing.Font("Century Gothic", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            okButton.Location = new System.Drawing.Point(ptToPutPictureBox.X, ptToPutPictureBox.Y);
            okButton.Name = "OkButton";
            okButton.Size = new System.Drawing.Size(70, 25);
            okButton.TabIndex = 35;
            okButton.Text = "OK";
            okButton.UseVisualStyleBackColor = true;
            okButton.Click += new EventHandler(okButton_Click);
            this.Controls.Add(okButton);
            this.Height = okButton.Location.Y + okButton.Height + 20;
        }

        void okButton_Click(object sender, EventArgs e)
        {
            if (numBlocksSelected == numBlocksToRemove)
                this.Close();
            else
                MessageBox.Show(this, "You must select " + numBlocksToRemove.ToString() + " to remove");
        }

        void picturebox_Click(object sender, EventArgs e)
        {
            if (blocksRemoved[(PictureBox)sender].removed)
            {
                BlockRemoved blockRemoved = blocksRemoved[(PictureBox)sender];
                blockRemoved.removed = false;
                blocksRemoved[(PictureBox)sender] = blockRemoved;
                numBlocksSelected--;
                ((PictureBox)sender).Invalidate();
            }
            else if (numBlocksSelected < numBlocksToRemove)
            {
                BlockRemoved blockRemoved = blocksRemoved[(PictureBox)sender];
                blockRemoved.removed = true;
                blocksRemoved[(PictureBox)sender] = blockRemoved;
                numBlocksSelected++;
                ((PictureBox)sender).Invalidate();
            }
        }

        void picturebox_Paint(object sender, PaintEventArgs e)
        {
            Bitmap b = new Bitmap(PlayingPieceGraphics.GetBlockSize().Width, PlayingPieceGraphics.GetBlockSize().Height);
            Graphics g = Graphics.FromImage(b);
            PlayingPieceGraphics.PaintBlock(g, blocksRemoved[(PictureBox)sender].block, new Point(PlayingPieceGraphics.GetBlockSize().Width / 2, PlayingPieceGraphics.GetBlockSize().Height / 2), 1, false);
            if (!blocksRemoved[(PictureBox)sender].removed)
            {
                for (int i = 0; i < b.Width; i++)
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (b.GetPixel(i, j).A != 0)
                        {
                            b.SetPixel(i, j, Color.FromArgb(128, b.GetPixel(i, j)));
                        }
                    }
                }
            }

            e.Graphics.DrawImage(b, new Point(0, 0));
        }

        public List<Block> GetListOfBlocksSelected()
        {
            List<Block> blocksSelected = new List<Block>();
            foreach(PictureBox pictureBox in blocksRemoved.Keys)
            {
                if (blocksRemoved[pictureBox].removed)
                    blocksSelected.Add(blocksRemoved[pictureBox].block);
            }
            return blocksSelected;
        }
    }
}